{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-25T07:33:19.868391Z",
     "start_time": "2025-02-25T07:33:19.794958Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import time"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T06:54:22.298159Z",
     "start_time": "2025-02-25T06:54:14.238253Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a=[]\n",
    "for _ in range(10**8):\n",
    "    a.append(random.random())\n",
    "print('-'*50)\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "b = np.array(a)\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "bae33d547477ba9c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "0.2696878910064697 0.055121421813964844\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.1 创建一维数组",
   "id": "c8d9001f4e728498"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T06:57:37.954525Z",
     "start_time": "2025-02-25T06:57:37.951581Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1 = [1,2,3,4]\n",
    "oneArray = np.array(list1) \n",
    "print(type(oneArray))  #对象类型\n",
    "print(list1)\n",
    "print(oneArray)\n",
    "\n",
    "# 创建数组的多种形式\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1,2,3]) \n",
    "print(t1) \n",
    "print(type(t1))\n",
    "'''\n",
    "[1 2 3]\n",
    "<class 'numpy.ndarray'> '''\n",
    "\n",
    "# 2. 传入range生成序列\n",
    "t2 = np.array(range(10)) \n",
    "print(t2) \n",
    "print(type(t2))\n",
    "'''\n",
    "[0 1 2 3 4 5 6 7 8 9]\n",
    "<class 'numpy.ndarray'> '''\n",
    "\n",
    "# 3. 使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0,10,2) \n",
    "print(t3) \n",
    "print(type(t3))"
   ],
   "id": "a2860d4ad3b9c7e5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1, 2, 3, 4]\n",
      "[1 2 3 4]\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T06:58:28.082997Z",
     "start_time": "2025-02-25T06:58:28.080209Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2 = [[1,2],[3,4],[5,6]]\n",
    "\n",
    "twoArray = np.array(list2) \n",
    "print(twoArray)"
   ],
   "id": "a6fc24afd23426e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:00:55.621314Z",
     "start_time": "2025-02-25T07:00:55.618304Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取数组的维度( 注意： 与函数的参数很像) \n",
    "print(twoArray.ndim)\n",
    "\n",
    "# 形状（行，列）\n",
    "print(twoArray.shape)\n",
    "\n",
    "# 有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "a2803f52330b6132",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.4 调整数组形状",
   "id": "969ed376da7979d6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:34:31.550301Z",
     "start_time": "2025-02-25T07:34:31.546301Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four = np.array([[1,2,3],[4,5,6]])\n",
    "\n",
    "# 修改的是原有的\n",
    "print(id(four))\n",
    "four.shape = (3,2) \n",
    "print(f'four={four},id(four)={id(four)}')\n",
    "\n",
    "# 返回一个新的数组\n",
    "four1 = four.reshape(3,2) \n",
    "print(four1)\n",
    "print(id(four1))"
   ],
   "id": "9d28ebae9847e720",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2995710573328\n",
      "four=[[1 2]\n",
      " [3 4]\n",
      " [5 6]],id(four)=2995710573328\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2995202587120\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:36:51.278337Z",
     "start_time": "2025-02-25T07:36:51.275337Z"
    }
   },
   "cell_type": "code",
   "source": [
    "five=four.reshape((6,))\n",
    "five"
   ],
   "id": "fe94aa0fb8f02f4a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:37:06.221237Z",
     "start_time": "2025-02-25T07:37:06.216731Z"
    }
   },
   "cell_type": "code",
   "source": [
    "six = four.flatten(order='C')\n",
    "six"
   ],
   "id": "3fc5c6c131f585ec",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:40:25.001557Z",
     "start_time": "2025-02-25T07:40:24.998053Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 拓展：数组的形状\n",
    "t = np.arange(24) \n",
    "print(t)\n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "t1 = t.reshape((2,3,4)) \n",
    "print(t1)\n",
    "print(t1.shape)\n",
    "t2=t1.reshape((24,))\n",
    "print(t2)"
   ],
   "id": "d89968874ec19d37",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.5 数组变列表",
   "id": "38f523009d97da7e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:43:19.964771Z",
     "start_time": "2025-02-25T07:43:19.961760Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将数组转成list\n",
    "a= np.array([9, 12, 88, 14, 25])\n",
    "list_a = a.tolist() \n",
    "list_a"
   ],
   "id": "f8009b4266738a13",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 12, 88, 14, 25]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5 数据类型",
   "id": "eb703aa4654953e6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:49:02.863915Z",
     "start_time": "2025-02-25T07:49:02.860264Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f = np.array([1,2,3,4,5],dtype=np.int16) \n",
    "print(f.itemsize) # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)\n",
    "\n",
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "\n",
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "print(round(random.random(),2))\n",
    "\n",
    "arr = np.array([random.random() for i in range(10)],dtype=np.float16)\n",
    "# 取小数点后两位\n",
    "print(np.round(arr,2))\n",
    "print(arr.dtype)"
   ],
   "id": "d48bc9a484e84d91",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n",
      "int64\n",
      "0.07\n",
      "[0.98 0.95 0.41 0.02 0.96 0.34 0.37 0.68 0.64 0.12]\n",
      "float16\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6 数组的计算",
   "id": "33e010ffc2690a43"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 6.1 数组和数",
   "id": "6648878fce8a570d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:52:22.789286Z",
     "start_time": "2025-02-25T07:52:22.784778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 =np.arange(24).reshape((6,4)) \n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2) \n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ],
   "id": "9d665fe8227d7d1e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:54:33.472867Z",
     "start_time": "2025-02-25T07:54:33.470361Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组与数组的计算\n",
    "t1 = np.arange(24).reshape((6,4))\n",
    "t2 = np.arange(100,124).reshape((6,4))\n",
    "\n",
    "print(t1+t2) \n",
    "print(\"-\"*20)\n",
    "print(t1-t2)"
   ],
   "id": "c458245ff6ea03f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "--------------------\n",
      "[[-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:55:09.729759Z",
     "start_time": "2025-02-25T07:55:09.721438Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#形状不一样的数组不能进行计算\n",
    "t1 = np.arange(24).reshape((4,6)) \n",
    "t2 = np.arange(18).reshape((3,6)) \n",
    "print(t1)\n",
    "print(t2) \n",
    "print(\"-\"*50)\n",
    "print(t1-t2) "
   ],
   "id": "6c2802a242e1e83e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (3,6) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[23], line 7\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2) \n\u001B[0;32m      6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m-\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m50\u001B[39m)\n\u001B[1;32m----> 7\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[43mt2\u001B[49m) \n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (4,6) (3,6) "
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:58:31.309689Z",
     "start_time": "2025-02-25T07:58:31.306389Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组尺寸不一样，其中一个数组的某一个维度的长度为1，可以进行计算\n",
    "t1 = np.arange(24).reshape((4,6)) \n",
    "t2 = np.arange(0,6).reshape((1,6)) \n",
    "print(t1)\n",
    "print(t2) \n",
    "print(\"-\"*50)\n",
    "print(t1-t2) "
   ],
   "id": "13a451523c92c0b1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0 1 2 3 4 5]]\n",
      "--------------------------------------------------\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:59:56.407445Z",
     "start_time": "2025-02-25T07:59:56.404517Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(4).reshape((4,1))\n",
    "print(t1)\n",
    "print(t2) \n",
    "print(\"-\"*50)\n",
    "print(t1-t2)"
   ],
   "id": "5ee28a58afd75b25",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:07:14.100577Z",
     "start_time": "2025-02-25T08:07:14.097638Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#6.3 练习轴\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "#按那个轴运算，哪个轴就会消失\n",
    "print(np.sum(a,axis=0)) \n",
    "\n",
    "print(np.sum(a,axis=1)) \n",
    "\n",
    "print(np.sum(a))"
   ],
   "id": "c380a2f082f4414e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:11:06.191824Z",
     "start_time": "2025-02-25T08:11:06.187824Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24) \n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "a = t.reshape((2,3,4)) \n",
    "a #最外面的方括号是0轴，最内层的方括号是最大轴"
   ],
   "id": "432a51f49ed2b4d6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(24,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[12, 13, 14, 15],\n",
       "        [16, 17, 18, 19],\n",
       "        [20, 21, 22, 23]]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:12:20.888077Z",
     "start_time": "2025-02-25T08:12:20.885084Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b=np.sum(a, axis=0)\n",
    "print(\"-\"*20)\n",
    "print(f'b{b.shape}')\n",
    "c=np.sum(a, axis=1)\n",
    "print(\"-\"*20)\n",
    "print(f'c{c.shape}')\n",
    "d=np.sum(a, axis=2)\n",
    "print(\"-\"*20)\n",
    "print(f'd{d.shape}')"
   ],
   "id": "9321450d2e4e0323",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n",
      "b(3, 4)\n",
      "--------------------\n",
      "c(2, 4)\n",
      "--------------------\n",
      "d(2, 3)\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 7 索引和切片",
   "id": "2185e8c223075086"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:15:26.495915Z",
     "start_time": "2025-02-25T08:15:26.493553Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2:4])\n",
    "\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2::2])"
   ],
   "id": "19d6a32d1d2bc750",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3]\n",
      "[2 4 6 8]\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:16:13.595903Z",
     "start_time": "2025-02-25T08:16:13.592430Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*'*20)\n",
    "print(t1[1]) # 取一行(一行代表是一条数据，索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*'*20)\n",
    "print(t1[1:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[1:3,:])# 取连续的多行\n",
    "print('*'*20)"
   ],
   "id": "124c90f991a741b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:20:46.912988Z",
     "start_time": "2025-02-25T08:20:46.908977Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(t1[[0,2,3]]) # 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3],:]) # 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[:,1]) # 取一列\n",
    "print('*'*20)\n",
    "print(t1[:,1:]) # 连续的多列\n",
    "print('*'*20)\n",
    "print(t1[:,[0,2,3]]) # 取不连续的多列\n",
    "print('*'*20)\n",
    "print(t1[2,3]) # 取某一个值,三行四列\n",
    "print('*'*20)\n",
    "print(t1[2:4,3:5]) # 取某一块区域\n",
    "print('*'*20)\n",
    "print(t1[[1,2],[0,2]]) # 取两个值"
   ],
   "id": "c56e23e4b5a40a21",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n",
      "[[15 16]\n",
      " [21 22]]\n",
      "********************\n",
      "[ 6 14]\n"
     ]
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 8 数组的修改",
   "id": "a46d229cbc855051"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T06:19:00.655774Z",
     "start_time": "2025-02-26T06:19:00.601513Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 1.修改某一行的值\n",
    "t[1, :] = 0\n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 2.修改某一列的值\n",
    "t[:, 1] = 0\n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 3.修改连续多行\n",
    "t[1:3, :] = 0 # 前闭后开\n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 4.修改连续多列\n",
    "t[:, 1:4] = 0 \n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 5.修改多行多列，取第二行到第四行，第三列到第五列\n",
    "t[1:4,2:5]=0\n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 6.修改多个不相邻的点\n",
    "t[[0,1],[2,3]]=0\n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 7.可以根据条件修改，比如将小于10的值改掉\n",
    "t[t < 10] = 0 # 方括号内的t指的是t的元素\n",
    "print(t)\n",
    "print('-'*20)\n",
    "# 8.使用逻辑判断\n",
    "# np.logical_and   & \n",
    "# np.logical_or   |\n",
    "# np.logical_not   ~\n",
    "t[(t > 2) & (t < 6)] = 0  # 与\n",
    "# t[(t < 2) | (t > 6)] = 0  # 或\n",
    "# t[~(t > 6)] = 0  # 非\n",
    "print(t)\n"
   ],
   "id": "bcc7c075cdf67286",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[[ 0  0  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [12  0 14 15 16 17]\n",
      " [18  0 20 21 22 23]]\n",
      "--------------------\n",
      "[[ 0  0  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0 20 21 22 23]]\n",
      "--------------------\n",
      "[[ 0  0  0  0  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0 22 23]]\n",
      "--------------------\n",
      "[[ 0  0  0  0  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "--------------------\n",
      "[[ 0  0  0  0  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "--------------------\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "--------------------\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "--------------------\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:41:23.347861Z",
     "start_time": "2025-02-25T08:41:23.344861Z"
    }
   },
   "cell_type": "code",
   "source": "t<10",
   "id": "c081ba974bb1178c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:41:26.513040Z",
     "start_time": "2025-02-25T08:41:26.510537Z"
    }
   },
   "cell_type": "code",
   "source": "t[t < 10]",
   "id": "1f01363479c61a35",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T06:25:04.675959Z",
     "start_time": "2025-02-26T06:25:04.670394Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "print(t)\n",
    "print('-'*20)\n",
    "t=t.clip(10,18) # 将不到下限的数字修改为下限，超过上限的数字修改为上限\n",
    "print(t)\n"
   ],
   "id": "f8bbb8b45e97fc28",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T06:27:42.966835Z",
     "start_time": "2025-02-26T06:27:42.950725Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)）\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "print(score)\n",
    "print('-'*20)\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result )"
   ],
   "id": "4b5086116426534c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "--------------------\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 9 数组的添加，删除，去重",
   "id": "b51f4aad58f06423"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T06:34:44.319459Z",
     "start_time": "2025-02-26T06:34:44.305414Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('-'*20)\n",
    "\n",
    "print('向数组添加元素：')\n",
    "# 使用append方法可以一次添加多个元素\n",
    "print(np.append(a, [7, 8, 9])) # 添加一维列表时，返回的也是一维列表\n",
    "# 注意append方法不会修改原来的数组\n",
    "print(a)\n",
    "print('-'*20)\n",
    "\n",
    "print('沿轴0添加元素：')\n",
    "print(np.append(a, [[7, 8, 9]], axis=0)) # 轴是谁，谁就变\n",
    "print('-'*20)\n",
    "\n",
    "print('沿轴1添加元素：')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n"
   ],
   "id": "5f0736e78163692",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------\n",
      "向数组添加元素：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------\n",
      "沿轴0添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "--------------------\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T06:46:56.125145Z",
     "start_time": "2025-02-26T06:46:56.110745Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. numpy.insert\n",
    "# 函数在给定索引之前，沿给定轴在输入数组中插入值。\n",
    "# 如果值的类型转换为要插入，则它与输入数组不同。\n",
    "# 插入没有原地的，函数会返回一个新数组。 此外，如果未提供轴，则输入数组会被展开。\n",
    "\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('-'*20)\n",
    "\n",
    "print('未传递Axis参数，在插入之前输入数组会被展开：')\n",
    "print(np.insert(a, 3, [11, 12])) # 在展开成一维的数组下标3位置插入新元素\n",
    "print('-'*20)\n",
    "print('传递了Axis参数，会广播值数组来配输入数组：')\n",
    "print('沿轴0广播：')\n",
    "print(np.insert(a, 1, [11,12], axis=0))\n",
    "print('沿轴1广播：')\n",
    "print(np.insert(a, 1, 11, axis=1))\n",
    "print('-'*20)\n",
    "print(a)"
   ],
   "id": "e61107c4b0b473be",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "--------------------\n",
      "未传递Axis参数，在插入之前输入数组会被展开：\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "--------------------\n",
      "传递了Axis参数，会广播值数组来配输入数组：\n",
      "沿轴0广播：\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "沿轴1广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n",
      "--------------------\n",
      "1770410580976\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T06:52:06.219669Z",
     "start_time": "2025-02-26T06:52:06.202202Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3,4)\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('-'*20)\n",
    "\n",
    "print('未传递Axis参数，在删除之前输入数组会被展开：')\n",
    "print(np.delete(a,5))\n",
    "print('-'*20)\n",
    "\n",
    "print('删除每一行中的第二列：')\n",
    "print(np.delete(a,1,axis = 1))"
   ],
   "id": "f1ee0420dd54412d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------\n",
      "未传递Axis参数，在删除之前输入数组会被展开：\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "--------------------\n",
      "删除每一行中的第二列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T06:54:26.023355Z",
     "start_time": "2025-02-26T06:54:26.015864Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('-'*20)\n",
    "\n",
    "print ('第一个数组的去重值：')\n",
    "u = np.unique(a)\n",
    "print (u)\n",
    "\n"
   ],
   "id": "2af8f21ec6056193",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "--------------------\n",
      "第一个数组的去重值：\n",
      "[2 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:04:56.671306Z",
     "start_time": "2025-02-26T07:04:56.663486Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(a) # 原数组\n",
    "# 1.置return_index为True\n",
    "u,indices = np.unique(a, return_index = True) # 左边是接收两个返回值的意思\n",
    "print ('去掉重复值后的数组：')\n",
    "print(u) # 去掉重复值后的数组\n",
    "print('得到的索引数组：')\n",
    "print(indices) # 返回的索引（下标）对应被保留下来的元素在原数组的下标\n",
    "print('-'*20)\n",
    "\n",
    "# 2.置return_inverse为True\n",
    "print(a)\n",
    "u,indices = np.unique(a,return_inverse = True)\n",
    "print('去掉重复值后的数组：')\n",
    "print(u)\n",
    "print('得到的索引数组：') \n",
    "# 返回了对应原来数组全部元素的索引，其索引值为原数组元素现在在新数组中的下标（如果被删除了那索引为0）\n",
    "print(indices)\n",
    "print('-'*20)\n",
    "\n",
    "# 3.置return_counts为True\n",
    "print(a)\n",
    "u,indices = np.unique(a,return_counts = True)\n",
    "print ('去掉重复值后的数组：')\n",
    "print(u)\n",
    "print('得到的索引数组：') \n",
    "# 返回了新数组中元素最初对应的重复值个数\n",
    "print(indices)"
   ],
   "id": "b2d5f112df891812",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去掉重复值后的数组：\n",
      "[2 5 6 7 8 9]\n",
      "得到的索引数组：\n",
      "[1 0 2 4 7 9]\n",
      "--------------------\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去掉重复值后的数组：\n",
      "[2 5 6 7 8 9]\n",
      "得到的索引数组：\n",
      "[1 0 2 0 3 1 2 4 0 5]\n",
      "--------------------\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去掉重复值后的数组：\n",
      "[2 5 6 7 8 9]\n",
      "得到的索引数组：\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10 数组的计算",
   "id": "e3c1246c80379577"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:58:34.984676Z",
     "start_time": "2025-02-25T08:58:34.982Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "# 1.获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "# 2.获取某一个轴上的数据最大值\n",
    "result = np.max(score,axis=0)\n",
    "print(result)\n",
    "# 3.获取1轴上的数据最小值\n",
    "result = np.min(score,axis=1)\n",
    "print(result)\n",
    "# 4.获取0轴上的数据最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result)\n"
   ],
   "id": "55463294c2e877dd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "[82 88]\n",
      "[80 81 75]\n",
      "[75 81]\n"
     ]
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:30:39.574590Z",
     "start_time": "2025-02-26T07:30:39.558133Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5. 数据的比较\n",
    "# result = np.maximum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较返回两者中的更大的那个\n",
    "# print(result)\n",
    "# result = np.minimum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "# print(result)\n",
    "# result = np.maximum( [5, -1, 0, 1, 2], [1, 2, 3, 4, 5] )\n",
    "# print(result)\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "\n",
    "# # 6. 求平均值\n",
    "# result = np.mean(score) # 获取所有数据的平均值\n",
    "# print(result)\n",
    "# result = np.mean(score,axis=0) # 获取某一行或者某一列的平均值\n",
    "# print(result)\n",
    "\n",
    "# 7. 求前缀和\n",
    "arr = np.array([[1,2,3], [4,5,6]])\n",
    "print(arr)\n",
    "print('-'*20)\n",
    "'''\n",
    "[1, 2, 3]------>   |1 |2 |3 |\n",
    "[4, 5, 6]------>   |5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(0)) # 指定0轴求前缀和\n",
    "print('-'*20)\n",
    "\n",
    "'''\n",
    "[1, 2, 3]------>   |1 |2+1   |3+2+1 |\n",
    "[4, 5, 6]------>   |4 |4+5   |4+5+6 |\n",
    "'''\n",
    "print(arr.cumsum(1)) # 指定1轴求前缀和\n"
   ],
   "id": "c3aa46d41c814a30",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "--------------------\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:33:31.893996Z",
     "start_time": "2025-02-26T07:33:31.879759Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(score)\n",
    "# print('-'*20)\n",
    "# \n",
    "# # 8. argmin求最小值索引\n",
    "# result = np.argmin(score,axis=0)\n",
    "# res=np.min(score,axis=0) #这样我们就可以知道最小的81是第二排的，是从前往后遍历的\n",
    "# print(result,res) # 同时打印最小值索引和最小值，并列在一行\n",
    "# print('-'*20)\n",
    "# \n",
    "# # 9. 求每一列的标准差（这里是总体标准差）\n",
    "# # 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# # 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "# result = np.std(score,axis=0)\n",
    "# print(result)\n",
    "# print('-'*20)\n",
    "\n",
    "# 10. 极 值\n",
    "result = np.ptp(score,axis=None)#就是最大值和最小值的差\n",
    "print(result)"
   ],
   "id": "5b93d3bc3843ba5b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "--------------------\n",
      "[2 1] [75 81]\n",
      "--------------------\n",
      "[2.94392029 3.29983165]\n",
      "--------------------\n",
      "13\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10 拼接和分割",
   "id": "1e658851e7740afc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:57:58.822316Z",
     "start_time": "2025-02-26T07:57:58.818280Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "print(a)\n",
    "print ('-' * 50)\n",
    "print(b)\n",
    "print ('-' * 50)\n",
    "# 要求a,b两个数组的维度相同\n",
    "print ('沿轴0连接两个数组：')\n",
    "print (np.concatenate((a,b),axis= 0))\n",
    "print ('-' * 50)\n",
    "print ('沿轴1连接两个数组：')\n",
    "print (np.concatenate((a,b),axis = 1))"
   ],
   "id": "83076b2b31e15df0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]]\n",
      "--------------------------------------------------\n",
      "[[5 6]\n",
      " [7 8]]\n",
      "--------------------------------------------------\n",
      "沿轴0连接两个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "--------------------------------------------------\n",
      "沿轴1连接两个数组：\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:02:12.084176Z",
     "start_time": "2025-02-26T08:02:12.080296Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 根据轴进行堆叠,会增加维度\n",
    "a =np.arange(12).reshape(3,4) # 最简单的创建二维数组的方法\n",
    "b =np.arange(12).reshape(3,4)\n",
    "print(a)\n",
    "print('-'*50)\n",
    "print(b)\n",
    "print('-'*50)\n",
    "# 使用stack方法连接数组\n",
    "print ('沿轴 0 连接两个数组：')\n",
    "print (np.stack((a,b),axis= 0)) \n",
    "print ('-'*50)\n",
    "print ('沿轴 1 连接两个数组：')\n",
    "print (np.stack((a,b),axis = 1))"
   ],
   "id": "5051c418a4ab530c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "沿轴 0 连接两个数组：\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]]\n",
      "--------------------------------------------------\n",
      "沿轴 1 连接两个数组：\n",
      "[[[ 0  1  2  3]\n",
      "  [ 0  1  2  3]]\n",
      "\n",
      " [[ 4  5  6  7]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [ 8  9 10 11]]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:05:56.487277Z",
     "start_time": "2025-02-26T08:05:56.482935Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3. 矩阵垂直拼接\n",
    "v1 = [[0,1,2,3,4,5],\n",
    "   [6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "   [18,19,20,21,22,23]]\n",
    "result = np.vstack((v1,v2)) # 使用vstack方法垂直拼接数组\n",
    "print(result)\n",
    "print('-'*50)\n",
    "# 4. 矩阵水平拼接\n",
    "v1 = [[0,1,2,3,4,5],\n",
    "   [6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "   [18,19,20,21,22,23]]\n",
    "result = np.hstack((v1,v2)) # 使用hstack方法水平拼接数组\n",
    "print(result)"
   ],
   "id": "6dd00d40e17d6862",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:08:26.595155Z",
     "start_time": "2025-02-26T08:08:26.566862Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数组的分割\n",
    "arr = np.arange(9).reshape(3,3)\n",
    "print(arr)\n",
    "print ('将数组分为三个大小相等的子数组：')\n",
    "b = np.split(arr,3,axis=0) # 使用split方法沿着0轴切割数组\n",
    "print(b)"
   ],
   "id": "6a4ecd55a3022311",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "将数组分为三个大小相等的子数组：\n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:14:08.097974Z",
     "start_time": "2025-02-26T08:14:08.093709Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1.hsplit沿着水平方向拆分\n",
    "harr = np.floor(10*np.random.random((2, 6))) #floor函数取整,ceil取上整,round四舍五入\n",
    "print ('原数组：')\n",
    "print(harr)\n",
    "\n",
    "print ('水平分割后：')\n",
    "print(np.hsplit(harr, 3)) # 平均分割成三块\n",
    "\n",
    "print ('-'*50)\n",
    "\n",
    "# 2.vsplit沿着垂直方向拆分\n",
    "a = np.arange(16).reshape(4,4)\n",
    "\n",
    "print ('原数组：')\n",
    "print (a)\n",
    "\n",
    "print ('竖直分割后：')\n",
    "b = np.vsplit(a,2) # 平均分割成两块\n",
    "print (b)\n"
   ],
   "id": "b8ae4be0ca93ccfa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[8. 7. 4. 0. 3. 9.]\n",
      " [7. 1. 2. 2. 3. 3.]]\n",
      "水平分割后：\n",
      "[array([[8., 7.],\n",
      "       [7., 1.]]), array([[4., 0.],\n",
      "       [2., 2.]]), array([[3., 9.],\n",
      "       [3., 3.]])]\n",
      "--------------------------------------------------\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "竖直分割后：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 12 nan和inf",
   "id": "7e213d54ea6cbb5b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:17:19.257291Z",
     "start_time": "2025-02-26T08:17:19.254284Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a))\n",
    "print(b,type(b))"
   ],
   "id": "dd21d82293067f88",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:21:09.371273Z",
     "start_time": "2025-02-26T08:21:09.359692Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan == np.nan # nan不等于nan",
   "id": "8e8e28e263bc2aa7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:22:28.678971Z",
     "start_time": "2025-02-26T08:22:28.645495Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf > -np.inf",
   "id": "823de838c21867c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:24:42.922279Z",
     "start_time": "2025-02-26T08:24:42.918661Z"
    }
   },
   "cell_type": "code",
   "source": [
    " # --判断数组中为nan的个数\n",
    "t = np.arange(24,dtype=float).reshape(4,6)\n",
    "print(t)\n",
    "print(\"-\"*50)\n",
    "# 将三行四列的数改成nan\n",
    "t[3,4] = np.nan\n",
    "print(t)\n",
    "print(\"-\"*50)\n",
    "print(t!=t)"
   ],
   "id": "a8cbebd0f14d4109",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "--------------------------------------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "--------------------------------------------------\n",
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:25:31.585249Z",
     "start_time": "2025-02-26T08:25:31.581788Z"
    }
   },
   "cell_type": "code",
   "source": "print(np.isnan(t)) # 判断是否为nan",
   "id": "d5f3c791fd052ce4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:33:36.056333Z",
     "start_time": "2025-02-26T08:33:36.051442Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24,dtype=float).reshape(4,6)\n",
    "t[3,4] = np.nan\n",
    "print(t)\n",
    "print(\"-\"*50)\n",
    "\n",
    "# 1.输出数组t当中非零元素的个数\n",
    "print(np.count_nonzero(t))\n",
    "# 2.输出数组t当中nan的个数\n",
    "print(np.count_nonzero(t != t)) \n",
    "# 3.将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "id": "27d7bbfd516c6d98",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "--------------------------------------------------\n",
      "23\n",
      "1\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:34:53.975883Z",
     "start_time": "2025-02-26T08:34:53.969703Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan+100",
   "id": "89e149de2919507e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:37:14.968152Z",
     "start_time": "2025-02-26T08:37:14.963656Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4,6).astype('float')\n",
    "print(t)\n",
    "print(\"-\"*50)\n",
    "# 将数组中的一部分替换nan\n",
    "t[1,3:] = np.nan\n",
    "print(t)\n",
    "print(\"-\"*50)\n",
    "# 遍历每一列，然后判断每一列是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "    # 按列遍历矩阵\n",
    "    # t.shape[1]对应矩阵t的列数6\n",
    "    # 获取当前列数据\n",
    "    temp_col = t[:,i]\n",
    "    \n",
    "    # 判断当前列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有nan\n",
    "    if nan_num != 0:\n",
    "      # 将这一列中不为nan的数据拿出来\n",
    "      temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "      \n",
    "      # 将所有为nan的元素替换成这一列中不为nan的数据的平均值\n",
    "      temp_col[np.isnan( temp_col )] = np.mean( temp_col_not_nan )\n",
    "      \n",
    "print(t)"
   ],
   "id": "211a6c18f4341b3c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "--------------------------------------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "--------------------------------------------------\n",
      "6\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的转置与轴滚动",
   "id": "ef4ce36d8b56e9c5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:46:20.943823Z",
     "start_time": "2025-02-26T08:46:20.939803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1.使用transpose函数做转置\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a )\n",
    "print ('-'*50)\n",
    "print ('对换数组：')\n",
    "print (np.transpose(a)) \n",
    "print ('-'*50)\n",
    "# 2.使用数组内置的T方法转置自身\n",
    "b = np.arange(12).reshape(3,4) # 与数组a一致\n",
    "print ('原数组：')\n",
    "print (b)\n",
    "print ('-'*50)\n",
    "print ('转置数组：')\n",
    "print (b.T)"
   ],
   "id": "23930149fe5dcb4c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "--------------------------------------------------\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:49:19.398738Z",
     "start_time": "2025-02-26T08:49:19.395101Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4,6)\n",
    "print (' 原数组 ：')\n",
    "print (t1)\n",
    "print ('-'*50)\n",
    "\n",
    "re = t1.swapaxes(1,0) # 交换轴，也算是一种转置\n",
    "print ('调用 swapaxes 函数后的数组：')\n",
    "print (re)"
   ],
   "id": "889e02f55a4530b5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原数组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:51:22.966848Z",
     "start_time": "2025-02-26T08:51:22.961684Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t=np.ones((3,4,5,6))\n",
    "\n",
    "t1=np.rollaxis(t,2,0)\n",
    "print(t1.shape)"
   ],
   "id": "931c2f20e5f655f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 3, 4, 6)\n"
     ]
    }
   ],
   "execution_count": 47
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
